<!doctype html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
    
  
  <link href="//cdn.jsdelivr.net/fancybox/2.1.5/jquery.fancybox.min.css" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






  

<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.6.2/css/font-awesome.min.css" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Maven," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="Maven是一个项目管理与整合工具。Maven为开发者提供了一套完整的构建生命框架，开发团队可以快速的自动完成工程的基础构建配置，应为Maven使用了一个标准的目录结构和一个默认的构建声明周期。
总的来说,Maven简化了工程的构建过程，并对其标准化。其衔接了编译，发布，文档生成，团队合作和其他的任务
Maven的主要目的是为开发者提供

一个可复用，可维护，更易理解的工程综合模型
与这个模型交互">
<meta property="og:type" content="article">
<meta property="og:title" content="Maven 学习笔记">
<meta property="og:url" content="https://zitongchen.github.io/2017/04/11/Maven-学习笔记/index.html">
<meta property="og:site_name" content="Tony's Notes">
<meta property="og:description" content="Maven是一个项目管理与整合工具。Maven为开发者提供了一套完整的构建生命框架，开发团队可以快速的自动完成工程的基础构建配置，应为Maven使用了一个标准的目录结构和一个默认的构建声明周期。
总的来说,Maven简化了工程的构建过程，并对其标准化。其衔接了编译，发布，文档生成，团队合作和其他的任务
Maven的主要目的是为开发者提供

一个可复用，可维护，更易理解的工程综合模型
与这个模型交互">
<meta property="og:updated_time" content="2017-09-16T08:49:38.747Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Maven 学习笔记">
<meta name="twitter:description" content="Maven是一个项目管理与整合工具。Maven为开发者提供了一套完整的构建生命框架，开发团队可以快速的自动完成工程的基础构建配置，应为Maven使用了一个标准的目录结构和一个默认的构建声明周期。
总的来说,Maven简化了工程的构建过程，并对其标准化。其衔接了编译，发布，文档生成，团队合作和其他的任务
Maven的主要目的是为开发者提供

一个可复用，可维护，更易理解的工程综合模型
与这个模型交互">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://zitongchen.github.io/2017/04/11/Maven-学习笔记/"/>





  <title> Maven 学习笔记 | Tony's Notes </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  














  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Tony's Notes</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://zitongchen.github.io/2017/04/11/Maven-学习笔记/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Tony">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avater.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Tony's Notes">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                Maven 学习笔记
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-04-11T00:00:00+08:00">
                2017-04-11
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Maven/" itemprop="url" rel="index">
                    <span itemprop="name">Maven</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/2017/04/11/Maven-学习笔记/" class="leancloud_visitors" data-flag-title="Maven 学习笔记">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数 </span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>Maven是一个项目管理与整合工具。Maven为开发者提供了一套完整的构建生命框架，开发团队可以快速的自动完成工程的基础构建配置，应为Maven使用了一个标准的目录结构和一个默认的构建声明周期。</p>
<p>总的来说,Maven简化了工程的构建过程，并对其标准化。其衔接了编译，发布，文档生成，团队合作和其他的任务</p>
<p>Maven的主要目的是为开发者提供</p>
<ul>
<li>一个可复用，可维护，更易理解的工程综合模型</li>
<li>与这个模型交互的插件或者工具</li>
</ul>
<p>Maven工程结构和内容被定义在一个XML文件中-pom.xml,是Project Object Model的简称，该文件是整个Maven系统的基础组件。</p>
<p>Maven是约定优于配置的，当创建Maven工程时，Maven会创建默认的工程结构，开发者只需把文件放到适当的工程结构便可。</p>
<p>以在MyEclipse 构建的一个Maven Javaweb为例说明Maven的工程结构</p>
<table>
<thead>
<tr>
<th style="text-align:left">结构目录</th>
<th style="text-align:left">配置文件</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">src/main/java</td>
<td style="text-align:left">Java源文件</td>
</tr>
<tr>
<td style="text-align:left">src/main/resources</td>
<td style="text-align:left">项目资源文件</td>
</tr>
<tr>
<td style="text-align:left">src/test/java</td>
<td style="text-align:left">测试源文件</td>
</tr>
<tr>
<td style="text-align:left">src/test/resources</td>
<td style="text-align:left">测试资源文件</td>
</tr>
<tr>
<td style="text-align:left">target</td>
<td style="text-align:left">编译文件等（自动生成）</td>
</tr>
<tr>
<td style="text-align:left">src/main/webapp</td>
<td style="text-align:left">Webapp文件</td>
</tr>
<tr>
<td style="text-align:left">……</td>
<td style="text-align:left">…..</td>
</tr>
</tbody>
</table>
<h3 id="安装Maven"><a href="#安装Maven" class="headerlink" title="安装Maven"></a>安装Maven</h3><ul>
<li>Maven是一个基于Java的工具，在使用Maven构建工程之前要配置Java的运行环境（关于如何安装Java运行环境自行百度）</li>
<li>下载Maven文件 <a href="http://maven.apache.org/download.cgi" target="_blank" rel="external">Maven 3.5.0 文件</a></li>
<li>解压Maven 文件，把解压文件放置在你想安装Maven的目录</li>
<li>设置Maven的环境变量，<code>M2_HOME={Maven的安装目录}</code>,把 <code>%M2_HOME%\bin</code>添加到系统<code>path</code>变量后面</li>
<li>在cmd命令窗口输入<code>mvn --version</code>（window系统）验证是否安装成功，若有Maven的版本信息输出证明安装成功</li>
</ul>
<h3 id="Maven-Pom"><a href="#Maven-Pom" class="headerlink" title="Maven Pom"></a>Maven Pom</h3><p>Pom代表工程对象模型，它是使用Maven工作时的基本组建，是一个xml格式文件。存放在工程的根目录下，并命名为pom.xml。POM中包含关于工程和配置信息，Maven使用这些信息构建工程</p>
<p>Pom举例</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">project</span> <span class="attr">xmlns</span>=<span class="string">"http://maven.apache.org/POM/4.0.0"</span> </span></div><div class="line">         <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span> </div><div class="line">         <span class="attr">xsi:schemaLocation</span>=<span class="string">"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"</span>&gt;</div><div class="line">  <span class="tag">&lt;<span class="name">modelVersion</span>&gt;</span>4.0.0<span class="tag">&lt;/<span class="name">modelVersion</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>MavenDemo<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span> #工程组名称</div><div class="line">  <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>MavenDemo<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span>  #工程标识</div><div class="line">  <span class="tag">&lt;<span class="name">version</span>&gt;</span>0.0.1-SNAPSHOT<span class="tag">&lt;/<span class="name">version</span>&gt;</span> #工程版本</div><div class="line">  <span class="tag">&lt;<span class="name">packaging</span>&gt;</span>war<span class="tag">&lt;/<span class="name">packaging</span>&gt;</span>  #package 类型</div><div class="line">  <span class="tag">&lt;<span class="name">name</span>&gt;</span>MavenDemo<span class="tag">&lt;/<span class="name">name</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">description</span>/&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">properties</span>&gt;</span>  #基本配置信息</div><div class="line">    <span class="tag">&lt;<span class="name">project.build.sourceEncoding</span>&gt;</span>UTF-8<span class="tag">&lt;/<span class="name">project.build.sourceEncoding</span>&gt;</span></div><div class="line">  <span class="tag">&lt;/<span class="name">properties</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">dependencies</span>&gt;</span> #jar包映射集</div><div class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span> #单个jar包映射</div><div class="line">      <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.glassfish<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>bean-validator<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.0-JBoss-4.0.2<span class="tag">&lt;/<span class="name">version</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">scope</span>&gt;</span>provided<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></div><div class="line">  <span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">build</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">plugins</span>&gt;</span>  #Maven插件集</div><div class="line">      <span class="tag">&lt;<span class="name">plugin</span>&gt;</span> #单个插件</div><div class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>maven-war-plugin<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.2<span class="tag">&lt;/<span class="name">version</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">configuration</span>&gt;</span></div><div class="line">          <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></div><div class="line">          <span class="tag">&lt;<span class="name">failOnMissingWebXml</span>&gt;</span>false<span class="tag">&lt;/<span class="name">failOnMissingWebXml</span>&gt;</span></div><div class="line">        <span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></div><div class="line">      <span class="tag">&lt;/<span class="name">plugin</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">plugins</span>&gt;</span></div><div class="line">  <span class="tag">&lt;/<span class="name">build</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">project</span>&gt;</span></div></pre></td></tr></table></figure>
<p>每个工程只有一个POM文件</p>
<ul>
<li>每个POM文件需要project元素和三个必须字段：groupId,artifactId,version</li>
<li>仓库的标识为groupId+artifactId+version</li>
</ul>
<h3 id="构建的生命周期"><a href="#构建的生命周期" class="headerlink" title="构建的生命周期"></a>构建的生命周期</h3><p>Maven构建工程会按照定义的阶段序列顺序执行每个阶段的注册目标，Maven有三个标准的生命周期：</p>
<ul>
<li>clean</li>
<li>default(or build)</li>
<li>site</li>
</ul>
<p>目标表示一个特定的，对构建和管理工程有帮助的任务，它可以有0个或多个构建阶段</p>
<p>例如：<code>mvn clean package</code>Maven指挥按顺序执行<code>clean -&gt; package</code>的构建阶段，而package以后的阶段maven不会执行</p>
<h4 id="clean的声明周期"><a href="#clean的声明周期" class="headerlink" title="clean的声明周期"></a>clean的声明周期</h4><ul>
<li>pre-clean</li>
<li>clean</li>
<li>post-clean</li>
</ul>
<p>Default(or Build)主要生命周期</p>
<table>
<thead>
<tr>
<th>生命周期</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>validate</td>
<td>检查工程配置是否正确</td>
</tr>
<tr>
<td>initialize</td>
<td>初始化构建状态，例如设置属性</td>
</tr>
<tr>
<td>complie</td>
<td>编译工程源码</td>
</tr>
<tr>
<td>test-complie</td>
<td>编译测试源代码到测试目录</td>
</tr>
<tr>
<td>test</td>
<td>使用适当的单元测试框架运行测试</td>
</tr>
<tr>
<td>package</td>
<td>获取编译后的代码，按照可发布的格式进行打包（war,jar）</td>
</tr>
<tr>
<td>verify</td>
<td>运行检查操作来验证工程包是有效的，并满足要求</td>
</tr>
<tr>
<td>install</td>
<td>安装工程包到本地仓库，该仓库可以作为本地其他项目的依赖</td>
</tr>
<tr>
<td>deploy</td>
<td>拷贝最终的工程包到远程仓库中，共享给其他工程人员</td>
</tr>
</tbody>
</table>
<h4 id="site-生命周期"><a href="#site-生命周期" class="headerlink" title="site 生命周期"></a>site 生命周期</h4><p>Maven site 插件一般用来创建新的报告文档，部署站点等</p>
<p>site的生命阶段：</p>
<ul>
<li>pre-site</li>
<li>site</li>
<li>post-site</li>
<li>site-deploy</li>
</ul>
<h3 id="Maven的配置文件"><a href="#Maven的配置文件" class="headerlink" title="Maven的配置文件"></a>Maven的配置文件</h3><p>配置文件是一组配置集合，是用来覆盖Maven构建的默认配置。使用配置文件可以为不同阶段构建过程，例如生产环境和部署环境等</p>
<p>Profile主要有三种类型</p>
<table>
<thead>
<tr>
<th>类型</th>
<th>定义文件的位置</th>
</tr>
</thead>
<tbody>
<tr>
<td>Per Pro</td>
<td>定义在每个工程的pom.xml中</td>
</tr>
<tr>
<td>Per User</td>
<td>定义在Maven设置的xml中(%user_home%/.m2/settings.xml</td>
</tr>
<tr>
<td>Global</td>
<td>定义在Maven全局配置文件中(%M2_HOME%/conf/settings.xml)</td>
</tr>
</tbody>
</table>
<p>Profile在pom.xml中使用activeProfiles/profiles元素指定</p>
<p>Profile 的激活主要有，显式的使用命令控制台输入，通过maven设置</p>
<ul>
<li><p>显示Profile激活</p>
<p>假定在E:\mvn目录下创建pom.xml文件</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">project</span> <span class="attr">xmlns</span>=<span class="string">"http://maven.apache.org/POM/4.0.0"</span></span></div><div class="line">  <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></div><div class="line">  <span class="attr">xsi:schemaLocation</span>=<span class="string">"http://maven.apache.org/POM/4.0.0</span></div><div class="line">  http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;</div><div class="line">  <span class="tag">&lt;<span class="name">modelVersion</span>&gt;</span>4.0.0<span class="tag">&lt;/<span class="name">modelVersion</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.companyname.projectgroup<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>project<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">profiles</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">profile</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">id</span>&gt;</span>test<span class="tag">&lt;/<span class="name">id</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">build</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">plugins</span>&gt;</span></div><div class="line">          <span class="tag">&lt;<span class="name">plugin</span>&gt;</span></div><div class="line">          <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.maven.plugins<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">          <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>maven-antrun-plugin<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">          <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.1<span class="tag">&lt;/<span class="name">version</span>&gt;</span></div><div class="line">          <span class="tag">&lt;<span class="name">executions</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">execution</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">phase</span>&gt;</span>test<span class="tag">&lt;/<span class="name">phase</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">goals</span>&gt;</span></div><div class="line">            	<span class="tag">&lt;<span class="name">goal</span>&gt;</span>run<span class="tag">&lt;/<span class="name">goal</span>&gt;</span></div><div class="line">            <span class="tag">&lt;/<span class="name">goals</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">configuration</span>&gt;</span></div><div class="line">              <span class="tag">&lt;<span class="name">tasks</span>&gt;</span></div><div class="line">                <span class="tag">&lt;<span class="name">echo</span>&gt;</span>Using env.test.properties<span class="tag">&lt;/<span class="name">echo</span>&gt;</span></div><div class="line">            <span class="tag">&lt;/<span class="name">tasks</span>&gt;</span></div><div class="line">        <span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></div><div class="line">        <span class="tag">&lt;/<span class="name">execution</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">executions</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">plugin</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">plugins</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">build</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">profile</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">profiles</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">project</span>&gt;</span></div></pre></td></tr></table></figure>
<p>打开命令控制台，转到pom.xml文件所在目录</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><div class="line">e:\mvn&gt;mvn test -Ptest  #在构建测设的时候使用 id为test的profile</div></pre></td></tr></table></figure>
<p>Maven将开始处理test Profile</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><div class="line">[INFO] Scanning for projects...</div><div class="line">[INFO]</div><div class="line">[INFO] ------------------------------------------------------------------------</div><div class="line">[INFO] Building project 1.0</div><div class="line">[INFO] ------------------------------------------------------------------------</div><div class="line">[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ project ---</div><div class="line">[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!</div><div class="line">[INFO] skip non existing resourceDirectory E:\mvn\src\main\resources</div><div class="line">[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ project ---</div><div class="line">[INFO] No sources to compile</div><div class="line">[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ project ---</div><div class="line">[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!</div><div class="line">[INFO] skip non existing resourceDirectory E:\mvn\src\test\resources</div><div class="line">[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ project ---</div><div class="line">[INFO] No sources to compile</div><div class="line">[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ project ---</div><div class="line">[INFO] No tests to run.</div><div class="line">[INFO] --- maven-antrun-plugin:1.1:run (default) @ project ---</div><div class="line">[INFO] Executing tasks</div><div class="line">     [echo] Using env.test.properties  #执行了echo命令行</div><div class="line">[INFO] Executed tasks</div><div class="line">[INFO] ------------------------------------------------------------------------</div><div class="line">[INFO] BUILD SUCCESS</div><div class="line">[INFO] ------------------------------------------------------------------------</div><div class="line">[INFO] Total time: 15.211 s</div><div class="line">[INFO] Finished at: 2017-04-11T16:24:12+08:00</div><div class="line">[INFO] Final Memory: 11M/119M</div><div class="line">[INFO] ------------------------------------------------------------------------</div></pre></td></tr></table></figure>
</li>
<li><p>maven激活</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">settings</span> <span class="attr">xmlns</span>=<span class="string">"http://maven.apache.org/POM/4.0.0"</span></span></div><div class="line">  <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></div><div class="line">  <span class="attr">xsi:schemaLocation</span>=<span class="string">"http://maven.apache.org/POM/4.0.0</span></div><div class="line">  http://maven.apache.org/xsd/settings-1.0.0.xsd"&gt;</div><div class="line">  <span class="tag">&lt;<span class="name">mirrors</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">mirror</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">id</span>&gt;</span>maven.dev.snaponglobal.com<span class="tag">&lt;/<span class="name">id</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">name</span>&gt;</span>Internal Artifactory Maven repository<span class="tag">&lt;/<span class="name">name</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">url</span>&gt;</span>http://repo1.maven.org/maven2/<span class="tag">&lt;/<span class="name">url</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">mirrorOf</span>&gt;</span>*<span class="tag">&lt;/<span class="name">mirrorOf</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">mirror</span>&gt;</span></div><div class="line">  <span class="tag">&lt;/<span class="name">mirrors</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">activeProfiles</span>&gt;</span></div><div class="line">  	<span class="tag">&lt;<span class="name">activeProfile</span>&gt;</span>test<span class="tag">&lt;/<span class="name">activeProfile</span>&gt;</span>  #定义了执行id为test的profile</div><div class="line">  <span class="tag">&lt;/<span class="name">activeProfiles</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">settings</span>&gt;</span></div></pre></td></tr></table></figure>
<p>在命令控制台跳转到pom.xml的目录执行</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><div class="line">e:\mvn&gt;mvn test</div></pre></td></tr></table></figure>
<h3 id="Maven仓库"><a href="#Maven仓库" class="headerlink" title="Maven仓库"></a>Maven仓库</h3><p>Maven仓库是存放项目jar和插件的地方</p>
<p>Maven仓库有三种类型：</p>
</li>
<li><p>本地(local)</p>
<p>Maven本地仓库是机器上的一个文件夹，默认存放在%user_home%目录下</p>
<p>修改<code>本地仓库默认位置</code>可以修改Maven的配置文件，在%M2_HOME%\conf目录中的settings.xml</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">settings</span> <span class="attr">xmlns</span>=<span class="string">"http://maven.apache.org/SETTINGS/1.0.0"</span></span></div><div class="line"><span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></div><div class="line"><span class="attr">xsi:schemaLocation</span>=<span class="string">"http://maven.apache.org/SETTINGS/1.0.0</span></div><div class="line">http://maven.apache.org/xsd/settings-1.0.0.xsd"&gt;</div><div class="line">	<span class="tag">&lt;<span class="name">localRepository</span>&gt;</span></div><div class="line">      &#123;在这里自定义仓库路径，例如：E:\workspace\mavenRepository&#125;</div><div class="line">  	<span class="tag">&lt;/<span class="name">localRepository</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">settings</span>&gt;</span></div></pre></td></tr></table></figure>
<p>当你运行Maven命令时，Maven将下载依赖jar包到指定的路径</p>
</li>
<li><p>中央(central)</p>
<p>Maven中央仓库是由Maven提供的仓库，当在本地找不到相应的jar包时，会联网到中央仓库下载相应jar到本地仓库</p>
</li>
<li><p>远程(remote)</p>
<p>当在本地跟中央仓库都找不到相应的jar包时，构建会显示错误，因此需要利用远程仓库下载项目依赖<br>须在pom.xml文件中添加下面内容</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">repositories</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">repository</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">id</span>&gt;</span>companyname.lib1<span class="tag">&lt;/<span class="name">id</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">url</span>&gt;</span>http://download.companyname.org/maven2/lib1<span class="tag">&lt;/<span class="name">url</span>&gt;</span></div><div class="line">  <span class="tag">&lt;/<span class="name">repository</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">repository</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">id</span>&gt;</span>companyname.lib2<span class="tag">&lt;/<span class="name">id</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">url</span>&gt;</span>http://download.companyname.org/maven2/lib2<span class="tag">&lt;/<span class="name">url</span>&gt;</span></div><div class="line">  <span class="tag">&lt;/<span class="name">repository</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">repositories</span>&gt;</span></div></pre></td></tr></table></figure>
</li>
</ul>
<p>Maven仓库引用顺序：本地仓库 -&gt; 中央仓库 -&gt; 远程仓库  这写仓库都没有便抛出异常  </p>
<h3 id="Maven插件"><a href="#Maven插件" class="headerlink" title="Maven插件"></a>Maven插件</h3><p>Maven实际上是依赖插件执行的框架，每个任务实际上是用插件完成的，插件一般用来：</p>
<ul>
<li>创建jar，war文件</li>
<li>编译代码文件</li>
<li>代码单元测试</li>
<li>创建工程文档</li>
<li>创建工程报告</li>
</ul>
<p>插件类型：</p>
<table>
<thead>
<tr>
<th>类型</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>Build plu</td>
<td>在构建时执行，并在pom.xml中配置</td>
</tr>
<tr>
<td>Reporting plugins</td>
<td>在网站生成过程中执行，并在pom.xml中配置</td>
</tr>
</tbody>
</table>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Maven/" rel="tag"># Maven</a>
          
        </div>
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/04/01/Python使用时掉进的坑/" rel="next" title="Python使用时掉进的坑">
                <i class="fa fa-chevron-left"></i> Python使用时掉进的坑
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/04/12/Hexo的NexT主题样式美化/" rel="prev" title="Hexo的NexT主题优化">
                Hexo的NexT主题优化 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>

          
          </div>
          


          
  <div class="comments" id="comments">
    
      <div id="uyan_frame"></div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avater.png"
               alt="Tony" />
          <p class="site-author-name" itemprop="name">Tony</p>
           
              <p class="site-description motion-element" itemprop="description">学习技术，分享技术...</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">29</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/categories/index.html">
                <span class="site-state-item-count">11</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">25</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/zitongchen" rel="external nofollow" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#安装Maven"><span class="nav-number">1.</span> <span class="nav-text">安装Maven</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Maven-Pom"><span class="nav-number">2.</span> <span class="nav-text">Maven Pom</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#构建的生命周期"><span class="nav-number">3.</span> <span class="nav-text">构建的生命周期</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#clean的声明周期"><span class="nav-number">3.1.</span> <span class="nav-text">clean的声明周期</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#site-生命周期"><span class="nav-number">3.2.</span> <span class="nav-text">site 生命周期</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Maven的配置文件"><span class="nav-number">4.</span> <span class="nav-text">Maven的配置文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Maven仓库"><span class="nav-number">5.</span> <span class="nav-text">Maven仓库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Maven插件"><span class="nav-number">6.</span> <span class="nav-text">Maven插件</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Tony</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io" rel="external nofollow">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next" rel="external nofollow">
    NexT.Mist
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    
    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  




  
  <script type="text/javascript" src="//cdn.jsdelivr.net/jquery/2.1.3/jquery.min.js"></script>

  
  <script type="text/javascript" src="//cdn.jsdelivr.net/fastclick/1.0.6/fastclick.min.js"></script>

  
  <script type="text/javascript" src="//cdn.jsdelivr.net/jquery.lazyload/1.9.3/jquery.lazyload.min.js"></script>

  
  <script type="text/javascript" src="//cdn.jsdelivr.net/velocity/1.2.3/velocity.min.js"></script>

  
  <script type="text/javascript" src="//cdn.jsdelivr.net/velocity/1.2.3/velocity.ui.min.js"></script>

  
  <script type="text/javascript" src="//cdn.jsdelivr.net/fancybox/2.1.5/jquery.fancybox.pack.js"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  



  




	





  





  
    

    
      <!-- UY BEGIN -->
      <script type="text/javascript" src="http://v2.uyan.cc/code/uyan.js?uid=2130825"></script>
      <!-- UY END -->
    
  





  



  
  

  

  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
  <script>AV.initialize("4wyUmphSKE5wrQGHEh5Js0bk-gzGzoHsz", "oTOHrhMuED5U0oqV2KrRMGF0");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  


  
</body>
</html>
